package com.woniu.elderly.operation.controller;

import com.mysql.cj.protocol.x.Notice;
import com.woniu.common.dto.OpenoticeDto;
import com.woniu.common.entity.Opeemployee;
import com.woniu.common.entity.Openotice;
import com.woniu.common.entity.PageBean;
import com.woniu.common.entity.ResponseResult;
import com.woniu.elderly.operation.service.OpenoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.stream.Collectors;

@RefreshScope
@Slf4j
@RestController
@RequestMapping("/notice")
public class OpenoticeController {
    @Autowired
    OpenoticeService openoticeService;



    @PostMapping("/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Openotice>> getAll(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody Openotice openotice){
        //log.info("pageIndex={},pageSize={},openotice={}",pageIndex,pageSize,openotice);
        PageBean<Openotice> pageBean = openoticeService.getNoticeList(pageIndex, pageSize, openotice);
        return ResponseResult.ok(pageBean);
    }

    @RequestMapping("/{nid}")
    public ResponseResult removeByNid(@PathVariable("nid") long nid){
        log.info("removeByNid={}",nid);
        openoticeService.deleteByNid(nid);
        return ResponseResult.ok("删除成功");
    }

    //像员工发送内部通知，如果不选择，则面向公众发送通知或公告
    @PostMapping("/send")
    public ResponseResult sendNotice(@RequestBody OpenoticeDto openoticeDto) {
        // 将DTO转换为Notice和Receiver对象
        Openotice openotice=new Openotice();
        // 设置notice属性...
        List<Opeemployee> receivers = openoticeDto.getEmpids().stream()
                .map(eid -> {
                    Opeemployee opeemployee = new Opeemployee();
                    opeemployee.setEid(eid);
                    // 设置receiver属性...
                    return opeemployee;
                })
                .collect(Collectors.toList());
        // 调用服务层发送通知的方法
        openoticeService.sendNotice(openotice, receivers);
        return ResponseResult.ok("发送成功");
    }

    @PostMapping("/")
    public ResponseResult addNotice(@RequestBody Openotice openotice){
        openoticeService.addNotice(openotice);
        return ResponseResult.ok("操作成功");
    }

    @PostMapping("/updateNotice")
    public ResponseResult updateNotice(@RequestBody Openotice openotice){
        openoticeService.updateNotice(openotice);
        return ResponseResult.ok("修改成功");
    }

    //在新的界面修改时使用
    @PostMapping("/{nid}")
    public ResponseResult getNoticeByNid(@PathVariable("nid")long nid){
        return ResponseResult.ok(openoticeService.getNoticeByNid(nid));
    }

    //展示文章内容
    @PostMapping("/getContentByNid/{nid}")
    public ResponseResult getContentByNid(@PathVariable("nid")long nid){
        return ResponseResult.ok(openoticeService.getContentByNid(nid));
    }


    @PostMapping("/nstate/{pageIndex}/{pageSize}")
    public ResponseResult<PageBean<Openotice>> getNoticeListByNstate(
            @PathVariable("pageIndex") int pageIndex,
            @PathVariable("pageSize") int pageSize,
            @RequestBody Openotice openotice){
        //log.info("pageIndex={},pageSize={},openotice={}",pageIndex,pageSize,openotice);
        PageBean<Openotice> pageBean = openoticeService.getNoticeListByNstate(pageIndex, pageSize, openotice);
        return ResponseResult.ok(pageBean);
    }

    //浏览量
    @GetMapping("/addViewCountById/{nid}")
    public ResponseResult addViewCountById(@PathVariable("nid")long nid){
        openoticeService.addViewCountById(nid);
        return ResponseResult.ok("阅读量加1");
    }

}
